package com.hmdp.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    @Override
    public List<ShopType> queryType() {
        //从Redis中查询商铺类型缓存
        Object sort=null;
        Object shopType = stringRedisTemplate.opsForHash().get("shop:type:", "id").toString();
        //判断是否存在
        if(shopType!=null){
            //存在，直接返回
            return (List<ShopType>) shopType;
        }
        //不存在，根据id查询数据库
        List<ShopType> shopTypeList = list();
        //不存在，返回错误
        if (shopTypeList==null){
            throw new RuntimeException("店铺类型不存在");
        }
        //存在，将商铺数据写入Redis
        stringRedisTemplate.opsForHash().put("shop:type:", "id", shopTypeList);
        //返回
        return shopTypeList;
    }
}
